﻿using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace Power.ViewModel.Dialog
{
    public class ProgressBarViewModel : ViewModelBase
    {
        private View.Dialog.ProgressBar model;
        private int progress;
        public int Progress
        {
            get
            {
                return progress;
            }
            set
            {

                progress = value;
                ProgressWidth = 510 * Progress / 100.0m;
                ProgressDesc = progress + "%";
                RaisePropertyChanged();
            }
        }
        private decimal progressWidth;
        public decimal ProgressWidth
        {
            get
            {
                return progressWidth;
            }
            set
            {

                progressWidth = value;
                RaisePropertyChanged();
            }
        }
        private string progressDesc;
        public string ProgressDesc
        {
            get
            {
                return progressDesc;
            }
            set
            {

                progressDesc = value;
                RaisePropertyChanged();
            }
        }
        public ProgressBarViewModel(View.Dialog.ProgressBar model)
        {
            this.model = model;
            Progress = 0;
            Task.Run(() =>
            {

                while (Progress < 100)
                {
                    Progress++;
                    Task.Delay(300).Wait();
                }
                if (Progress >= 100) {
                    App.Current.Dispatcher.Invoke((Action)delegate ()
                    {
                        this.model.DialogResult = true;
                        this.model.Close();
                    });
                }
            });
        }


    }
}
